﻿using System.Windows.Controls;
using System;

namespace MMK.Paket_Tracker.Runtime.ValidationRules
{
    /// <summary>
    /// 
    /// </summary>
    public class StringValidationRule : ValidationRule
    {
        #region public methods
        /// <summary>
        /// Initializes a new instance of the <see cref="StringValidationRule"/> class.
        /// </summary>
        public StringValidationRule() : base()
        {
            MinLength = 3;
            MaxLength = 20;
        }

        /// <summary>
        /// When overridden in a derived class, performs validation checks on a value.
        /// </summary>
        /// <param name="value">The value from the binding target to check.</param>
        /// <param name="cultureInfo">The culture to use in this rule.</param>
        /// <returns>
        /// A <see cref="T:System.Windows.Controls.ValidationResult"/> object.
        /// </returns>
        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            ValidationResult result;
            string s = (value as string) ?? String.Empty;

            if (s.Length < MinLength)
            {
                result = new ValidationResult(false, "Eingabe zu kurz");
            }
            else if (s.Length > MaxLength)
            {
                result = new ValidationResult(false, "Eingabe zu lang");
            }
            else
            {
                result = ValidationResult.ValidResult;
            }

            return result;

        }

        #endregion

        #region public properties

        /// <summary>
        /// Gets or sets the minimum length
        /// </summary>
        /// <value>The minimum length.</value>
        public Int32 MinLength { get; set; }
        /// <summary>
        /// Gets or sets the maximum length.
        /// </summary>
        /// <value>The maximum length.</value>
        public Int32 MaxLength { get; set; }

        #endregion
    }
}
